using Eno.Application.Dto;
using Eno.Domain.Entity.App;
using Eno.Domain.Repositories;

namespace Eno.Application.Services;

public class PermissionService
{
    private readonly IPermissionRepository<AppPermission> _rep;

    public PermissionService(IPermissionRepository<AppPermission> permissionRepository)
    {
        _rep = permissionRepository;
    }

    public async Task<IEnumerable<AppPermission>> GetAppPermissionsAsync()
    {
        return await _rep.GetAppPermissionAsync();
    }


    public async Task<AppPermission?> GetAppPermissionById(Guid permissionId)
    {
        return await _rep.GetPermissionByIDAsync(permissionId);
    }


    public async Task<int> DeletePermissionAsync(Guid permissionId)
    {
        return await _rep.DeletePermissionAsync(permissionId);
    }

    public async Task<int> UpdatePermissionAsync(Guid permissionId, PermissionDto permissionDto)
    {
        var permission = await _rep.GetPermissionByIDAsync(permissionId);
        if (permission == null) return 0;
        permission.PermissionName = permissionDto.PermissionName;
        return await _rep.UpdatePermissionAsync(permission);
    }

    public async Task<int> AdditionPermissionAsync(string PermissionName)
    {
        var permissions = await _rep.GetAppPermissionAsync();
        var exists = permissions.Any(p => p.PermissionName == PermissionName);
        if (exists)
        {
            return 0;
        }
        var permission = new AppPermission {ID = Guid.NewGuid(),PermissionName = PermissionName };
        return await _rep.AdditionPermissionAsync(permission);
    }

    public async Task<IEnumerable<AppRole>> GetRolesByPermissionId(Guid permissionId)
    {
        return await _rep.GetRolesByPermissionIdAsync(permissionId);
    }
}